package com.irdstudio.efp.esb.service.impl.sed.ler;

import com.alibaba.fastjson.JSONObject;
import com.irdstudio.basic.framework.core.constant.ESBConstant;
import com.irdstudio.basic.framework.core.exception.ESBException;
import com.irdstudio.efp.esb.service.bo.req.sed.ler.ReqLoanExecRateBean;
import com.irdstudio.efp.esb.service.bo.resp.sed.ler.RespLoanExecRate;
import com.irdstudio.efp.esb.service.client.ESBClientFactory;
import com.irdstudio.efp.esb.service.facade.sed.ler.SedLoanExecRateService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;


/**
 * 需求编号：【2019D0519】
 * 问题编号：【】
 * 开发人员：xiaojunhui
 * 创建日期：2019/5/5 16:20
 * 功能描述：05（贷款执行利率查询）
 * @version 0.1
 * @date 2019/5/5 16:20
 */
@Service("sedLoanExecRateService")
public class SedLoanExecRateServiceImpl implements SedLoanExecRateService {

    private static Logger logger = LoggerFactory.getLogger(SedLoanExecRateService.class);

    @Override
    public RespLoanExecRate queryLoanExecRate(ReqLoanExecRateBean reqLoanExecRateBean) throws ESBException {
        //校验参数 MBSD_BP_LM-1400-0110
        if (reqLoanExecRateBean==null) {
            throw new RuntimeException("请求参数对象不能为空");
        }
        if (StringUtils.isEmpty(reqLoanExecRateBean.getLoanTrm())) {
            throw new RuntimeException("贷款期限不能为空");
        }

        //设置服务领域
        if (StringUtils.isEmpty(reqLoanExecRateBean.getSvcFld())) {

            reqLoanExecRateBean.setSvcFld("MBSD_BP_LM");
        }

        //设置服务类型
        if (StringUtils.isEmpty(reqLoanExecRateBean.getSvcTp())) {
            reqLoanExecRateBean.setSvcTp("1400");
        }

        //互金服务编码
        if (StringUtils.isEmpty(reqLoanExecRateBean.getServiceCd())) {

            reqLoanExecRateBean.setServiceCd("0110");
        }
        //发起请求
        logger.info("查询执行利率开始发送esb请求，请求参数为："+ JSONObject.toJSONString(reqLoanExecRateBean));
        RespLoanExecRate respLoanExecRate = null;
        try {
            respLoanExecRate = ESBClientFactory.buildClient()
                    .withBody(reqLoanExecRateBean)
                    .withTradeNo(ESBConstant.TRADE_CODE_30230003)
                    .withScene(ESBConstant.SCENE_NO_05)
                    .create()
                    .sendAndReceive()
                    .getBody(RespLoanExecRate.class);
            if(respLoanExecRate != null) {
                logger.debug("查询执行利率接口结束，响应结果为：" + JSONObject.toJSONString(respLoanExecRate));
            }
        } catch (ESBException e) {
            logger.error("查询执行利率发生异常，异常信息为："+e.getMessage());
            throw e;
        }
        //返回结果
        return respLoanExecRate;
    }


}
